import {
	dxrequest,
} from "./index.js"
// 小程序登录方法
export const wxlogin = () => {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync("token")
		if (!token) {
			uni.login({
				provider: 'weixin', //使用微信登录
				success: async (loginRes) => {
					const code = loginRes.code
					const {
						data
					} = await dxrequest('wechar/xcx/getOpenId', {
						code
					})
					const res = await dxrequest('wechar/xcx/userinfo/login', {
						...data
					})
					uni.setStorageSync("openid",data.openid)
					uni.setStorageSync("token", res.data.token)
					if(res.data.phone){
							uni.setStorageSync("phone",res.data.phone)
					}
					resolve(data)
				}
			});
		}else{
			resolve({
				token,
				phone:uni.getStorageSync("phone")
			})
		}
		
	})
}